/*
 * link.c
 *
 *  Created on: 2023年8月24日
 *      Author: HXF
 */
#include "link.h"
#include "sl_malloc.h"
#include "string.h"
#include "app_log.h"

#define LINK_API_MALLOC  sl_malloc
#define LINK_API_FREE  sl_free

link_t *link_creathead(void)
{
  link_t *node = LINK_API_MALLOC(sizeof(link_t));
  if(!node){
      return NULL;
  }else{
      memset(node, 0, sizeof(link_t));
  }

  node->data = NULL;
  node->next = NULL;
  return node;
}


link_t *link_creatnode(void *data, int len)
{
  if(!data || len == 0){
      return NULL;
  }
  link_t *node = LINK_API_MALLOC(sizeof(link_t));
  if(!node){
      DS_app_log_error("malloc faile\r\n");
      return NULL;
  }else{
      memset(node, 0, sizeof(link_t));
  }
  node->data = LINK_API_MALLOC(len);
  if(node->data){
      node->len = len;
      memcpy(node->data, data, len);
      node->next = NULL;
  }else{
      DS_app_log_error("malloc faile\r\n");
      LINK_API_FREE(node);
      node = NULL;
  }
  return node;
}

int link_push_head(link_t *head, void *data, int len)
{
  if(!head || !data || len == 0){
      return 1;
  }

  link_t *node = link_creatnode(data, len);
  if(!node){
      return 1;
  }
  node->next = head->next;
  head->next = node;
  return 0;
}

int link_push_end(link_t *head, void *data, int len)
{
  if(!head || !data || len == 0){
      return 1;
  }

  link_t *node = head;
  while(node->next){
      node = node->next;
  }
  node->next = link_creatnode(data, len);
  return 0;
}

int link_pop(link_t *head, void *data, int *len)
{
  if(!head || !data || !len){
      return 1;
  }
  link_t *node = head->next;
  if(node){
      *len = node->len;
      if(*len){
          memcpy(data, node->data, node->len);
      }
      if(node->data){
          LINK_API_FREE(node->data);
          node->data = NULL;
      }
      head->next = node->next;
      LINK_API_FREE(node);
      node = NULL;
  }else{
  }
  if(*len == 0){
      return 2;
  }
  return 0;
}

int link_len(link_t *head)
{
  int len = 0;

  if(!head){
      return 0;
  }
  link_t *node = head->next;
  while(node){
      node = node->next;
      len++;
  }
  return len;
}

int link_find_by_idx(link_t *head, void *data, int *ptr_len, int idx)
{
  int i = 0;

  if(!head || !head->next){
      return 1;
  }
  link_t *node = head->next;
  while(node){
      if(i == idx){
          break;
      }else{
          i++;
          node = node->next;
      }
  };
  if(i < idx){
      return 1;
  }
  *ptr_len = node->len;
  if(*ptr_len == 0){
      return 2;
  }
  memcpy(data, node->data, *ptr_len);
  return 0;
}
#if(0)
int link_find_by_pageid(link_t *head, void *data, int data_len,
                        unsigned char *pageid)
{
  if(!head || !head->next){
      return 1;
  }
  link_t *node = head->next;
  cache_page_file_t *file = NULL;
  while(node){
      file = node->data;
      if((file->page_id[0] == pageid[0]) && (file->page_id[1] == pageid[1])){
          break;
      }else{
          node = node->next;
      }
  };
  if(!node){
      return 1;
  }
  memcpy(data, node->data, data_len);
  return 0;
}
#endif
int link_delte_all_node(link_t *head)
{
  if(!head){
      return 1;
  }
  link_t *node = head->next;
  while(node){
      head->next = node->next;
      if(node->data){
          LINK_API_FREE(node->data);
          node->data = NULL;
      }
      LINK_API_FREE(node);
      node = NULL;
      node = head->next;
  }
  return 0;
}
